﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.Packet;
using System.Net;

namespace Hi_Baidu.FileShare.Packet.InPacket
{
    /// <summary>
    ///     发送文件请求包
    /// </summary>
    public class RequestSendFilePacket : SendFileInPackage
    {
        public RequestSendFilePacket(InPackage inpackage) : base(inpackage)
        {

        }

        public override void InitPackage()
        {
            int Offset = 0;
            //用户名长度
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //用户名
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //文件名长度
            byte[] FileNameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FileNameLength, 0, 4);
            Offset += 4;
            //文件名
            byte[] FileName = new byte[BitConverter.ToInt32(FileNameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FileName, 0, FileName.Length);
            Offset += FileName.Length;
            //会话长度
            byte[] SessionLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, SessionLength, 0, 4);
            Offset += 4;
            //会话
            byte[] Session = new byte[BitConverter.ToInt32(SessionLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Session, 0, Session.Length);
            Offset += Session.Length;
            //文件后缀名长度
            byte[] ExtLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ExtLength, 0, 4);
            Offset += 4;
            //文件后缀名
            byte[] Ext = new byte[BitConverter.ToInt32(ExtLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Ext, 0, Ext.Length);
            Offset += Ext.Length;
            //文件大小
            byte[] FileSize = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FileSize, 0, 4);
            Offset += 4;
            //IP地址长度
            byte[] IPLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPLength, 0, 4);
            Offset += 4;
            //IP地址
            byte[] IPAddress = new byte[BitConverter.ToInt32(IPLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPAddress, 0, IPAddress.Length);
            Offset += IPAddress.Length;
            //文件端口
            byte[] FilePort = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FilePort, 0, 4);
            Offset += 4;

            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("FILENAME", FileName);
            this.DIC_CONTENT.Add("SESSION", Session);
            this.DIC_CONTENT.Add("EXT", Ext);
            this.DIC_CONTENT.Add("FILESIZE", FileSize);
            this.DIC_CONTENT.Add("IPADDRESS", IPAddress);
            this.DIC_CONTENT.Add("FILEPORT", FilePort);
        }

        /// <summary>
        ///     获得文件发送者用户名
        /// </summary>
        public String GetFileSendUsername()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["USERNAME"]);
        }

        /// <summary>
        ///     获得文件后缀名
        /// </summary>
        public String GetFileExt()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["EXT"]);
        }

        /// <summary>
        ///     获得文件会话ID
        /// </summary>
        public String GetFileSession()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["SESSION"]);
        }

        /// <summary>
        ///     获得文件名称
        /// </summary>
        public String GetFileName()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["FILENAME"]);
        }

        /// <summary>
        ///     获得IP地址
        /// </summary>
        public IPAddress GetIPAddress()
        {
            return IPAddress.Parse(Encoding.Default.GetString(this.DIC_CONTENT["IPADDRESS"]));
        }

        /// <summary>
        ///     获得文件大小
        /// </summary>
        public int GetFileSize()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["FILESIZE"], 0);
        }

        /// <summary>
        ///     获得文件端口
        /// </summary>
        public int GetFilePort()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["FILEPORT"], 0);
        }
    }
}
